﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Web;
using System.Text.RegularExpressions;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class public_html_NewSite_CMSV2_NewsAdd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ReviewerMySql SQL = new ReviewerMySql();
        if (!IsPostBack)
        {
            TxtID.Text = Guid.NewGuid().ToString();
            OdbcDataReader reader = SQL.ExecuteReader("SELECT Titel AS SpilTitel, SpilID AS SpilSpilID FROM V2_Spil GROUP BY SpilID ORDER BY Titel");
            while (reader.Read())
            {
                ListGames.Items.Add(new ListItem(reader["SpilTitel"].ToString(), reader["SpilSpilID"].ToString()));
            }
        }
    }

    protected void BtmSave_Click(object sender, EventArgs e)
    {
        ReviewerMySql SQL = new ReviewerMySql();

        string Tekst = TxtTekst.Text;

        #region Security module replacement hack.
        int userId = -1;
        using (IDataReader reader = SQL.ExecuteReader("SELECT ID FROM v2_writters WHERE Navn = '" + Page.User.Identity.Name + "'"))
        {
            if (reader.Read())
            {
                userId = reader.GetInt32(0);
            }
        }
        if (userId < 0)
        {
            throw new InvalidOperationException("Failed to retrieve userid");
        }
        #endregion

        SQL.ExecuteNonQuery("INSERT INTO V2_Nyhed(NewsID, Titel, Header, Tekst, Kilde, Screens, Emne, Forfatter) VALUES('" + TxtID.Text + "','" + TxtTitel.Text + "','" + Regex.Replace(TxtHeader.Text, "'","''") + "','" + Regex.Replace(Tekst, "'", "''", RegexOptions.IgnoreCase) + "','" + TxtKilde.Text + "','" + ChkScreens.Checked.ToString() + "','" + DropEmne.SelectedItem.ToString() + "'," + userId.ToString() + ")");

        foreach (ListItem item in ListRelations.Items)
        {
            string NewsID = TxtID.Text;
            string SpilID = item.Value.ToString();
            SQL.ExecuteNonQuery("INSERT INTO V2_NewsRelations(NewsID, SpilID) VALUES('" + NewsID + "','" + SpilID + "')");
        }

        for (int i = 0; i < ListFormat.Items.Count; i++)
        {
            if (ListFormat.Items[i].Selected)
            {
                SQL.ExecuteNonQuery("INSERT INTO V2_NewsFormat(Format, NewsID) VALUES('" + ListFormat.Items[i].Value.ToString() + "','" + TxtID.Text + "')");
            }
        }

        if (FileUploadNews.HasFile)
        {
            string Path = Server.MapPath("../news/");
            FileUploadNews.SaveAs(@Path + TxtID.Text + ".jpg");
        }
        //Forside snask
        OdbcDataReader TempReaaader = SQL.ExecuteReader("SELECT ID FROM V2_Nyhed ORDER BY ID DESC LIMIT 1");
        string ID;
        string HeaderToFunc = "";
        string HeaderText = TxtHeader.Text;
        if(HeaderText.Length > 150) 
        {
            HeaderToFunc = HeaderToFunc.Substring(0,150) + "...";
        }
        else 
        {
            HeaderToFunc = HeaderText;
        }
        TempReaaader.Read();
        string test = FrontPageAdmin.CreateFrontPage(TxtTitel.Text, "nyhed", HeaderToFunc, TempReaaader[0].ToString());
        Response.Redirect("Message.aspx?MSG=Nyheden blev gemt");
    }
    protected void Add_Click(object sender, EventArgs e)
    {
        string Titel = ListGames.SelectedItem.ToString();
        string SpilID = ListGames.SelectedValue.ToString();
        ListRelations.Items.Add(new ListItem(Titel, SpilID));
        ListGames.Items.RemoveAt(ListGames.SelectedIndex);
    }
    protected void Remove_Click(object sender, EventArgs e)
    {
        ListGames.Items.Add(new ListItem(ListRelations.SelectedItem.ToString(), ListRelations.SelectedValue));
        ListRelations.Items.RemoveAt(ListRelations.SelectedIndex);
    }
}
